package com.ygxsk.carhome.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.blankj.utilcode.util.LogUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.ygxsk.carhome.event.EventPay;
import com.ygxsk.carhome.helper.PushConstants;
import com.ygxsk.carhome.sys.Session;

import org.greenrobot.eventbus.EventBus;

//import android.app.AlertDialog;
//import android.util.Log;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    //private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.pay_result);

        Session.pushOneActivity(this);

        api = WXAPIFactory.createWXAPI(this, PushConstants.WEIXIN_APPID);

        if (api != null) {
            api.handleIntent(getIntent(), this);
        }

        //LogUtils.d("itchen------WXPayEntryActivity-------onCreate");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);

        if (api != null) {
            api.handleIntent(intent, this);
        }

        //LogUtils.d("itchen--onNewIntent");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.d("itchen--onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.d("itchen------WXPayEntryActivity-------onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.d("itchen------WXPayEntryActivity-------onDestroy");
        //itchen------WXPayEntryActivity-------onDestroy
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
    }

    private void methodOnSuccess(int _code) {

        EventBus.getDefault().post(new EventPay(_code));

        if (!isFinishing()) {
            WXPayEntryActivity.this.finish();
        }

    }

    private void methodOnFail(int _code, String error) {

        EventBus.getDefault().post(new EventPay(_code, error));

        if (!isFinishing()) {
            WXPayEntryActivity.this.finish();
        }

    }

    @Override
    public void onResp(BaseResp resp) {

        //Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        LogUtils.d("itchen---resp.errCode=" + resp.errCode);
        //itchen---resp.errCode=0
        LogUtils.d("itchen---resp.errCode=" + resp.errStr);
        //itchen---resp.errCode=null
        // itchen---resp.errCode=null

        //微信支付的通知处理

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            int errorCode = resp.errCode;

            if (errorCode == BaseResp.ErrCode.ERR_OK) {

                methodOnSuccess(errorCode);
                LogUtils.d("itchen--onResp--errorCode成功=" + errorCode + "-->" + resp.errStr);//itchen--onResp--errorCode=-2-->null
//                itchen--onResp--errorCode成功=0-->null
                //itchen--onResp--errorCode成功=0-->null
            } else {

                LogUtils.d("itchen--onResp--errorCode失败=" + errorCode + "-->" + resp.errStr);
                String error = resp.errStr;//这个成null了.
                methodOnFail(errorCode, error);

            }

        }


        //微信支付的回调提示信息。
		/*if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_tip);
			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
			builder.show();
		}*/
    }
}